const connections = require("../app/database");

class ServiceLabel {
  //创建标签
  async createLabel(name) {
    try {
      const sql = ` INSERT INTO label (name) VALUES (?)`;
      const result = await connections.execute(sql, [name]);
      return result[0];
    } catch (e) {
      console.log(e);
    }
  }
//查询标签
  async getLabelByName(name) {
    try {
      const statement = `SELECT * FROM label WHERE name = ?;`
      const result = await connections.execute(statement, [name]);
      return result[0];
    } catch (e) {
      console.log(e);
    }
  }
  // //查询动态中已有的标签
  // async hasLabel(momentId, labelId) {
  //   try {
  //     const statement = `SELECT * FROM moment_label WHERE moment_id = ? AND label_id = ?;`
  //     const result = await connections.execute(statement, [momentId, labelId]);
  //     return result[0];
  //   } catch (e) {
  //     console.log(e);
  //   }
  // }
  async getLabels(limit, offset) {
    try {
      const statement = `SELECT * FROM label LIMIT ? OFFSET ?;`
      const result = await connections.execute(statement, [limit, offset]);
      return result[0];
    } catch (e) {
      console.log(e);
    }
  }
}

module.exports = new ServiceLabel();
